package conexion;

import rmi.ICPRRemote;
import rmi.IODVRemote;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;

import VO.ClienteVO;
import VO.CondicionesPagoVO;
import VO.CotizacionVO;
import VO.FacturaVO;
import VO.ItemListaPreciosVO;
import VO.ListaPreciosCPRVO;
import VO.ListaPreciosVO;
import VO.MovimientoStockVO;
import VO.ProveedorVO;
import VO.RodamientoVO;
import VO.SolicitudCompraCPRVO;
import VO.SolicitudRodamientosVO;

public class ProxyIR extends UnicastRemoteObject implements ICPRRemote,IODVRemote {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7481740909974536570L;
	private static ICPRRemote cprir;
	private static ProxyIR instance;
	private IODVRemote odvir;
		
		private ProxyIR()throws MalformedURLException, NotBoundException,RemoteException {
		}	

		public static ProxyIR getInstance()
		{
			if(instance==null)
			{
				try{
					instance=new ProxyIR();
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NotBoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return instance;
		}
		
		private void reconnectCPR()
		{
			try {
				String location="//localhost/CPRServer";
				cprir =(ICPRRemote) Naming.lookup(location);
				System.out.println("conectado a servidor CPR remoto en:" + location);
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NotBoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		public ICPRRemote getCPRIR() {
			if(cprir==null)
			{
				reconnectCPR();
			}
			try{
				//si falla la conectividad levanto una nueva conexion.
				cprir.conectivityTest();
			}
			catch(Exception e)
			{
				reconnectCPR();
			}
			return cprir;
		}
		public IODVRemote getODVIR() {
			if(odvir==null)
			{
				reconnectODV();
			}
			try{
				//si falla la conectividad levanto una nueva conexion.
				odvir.conectivityTestODV();
			}
			catch(Exception e)
			{
				reconnectCPR();
			}
			return odvir;
		}

		private void reconnectODV() {
			try {
				String location="//localhost/CPRServer";
				odvir =(IODVRemote) Naming.lookup(location);
				System.out.println("conectado a servidor ODV remoto en:" + location);
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NotBoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
		}

		@Override
		public void conectivityTest() throws RemoteException {
			getCPRIR().conectivityTest();	
			
		}

		@Override
		public ClienteVO obtenerCliente(String cuit) throws RemoteException {
			// TODO Auto-generated method stub
			return getCPRIR().obtenerCliente(cuit);
		}

		@Override
		public List<RodamientoVO> obtenerRodamientos() throws RemoteException {
			// TODO Auto-generated method stub
			return getCPRIR().obtenerRodamientos();
		}

		@Override
		public int cargarSolicitudRodamientos(String xml)
				throws RemoteException {
			// TODO Auto-generated method stub
			return getODVIR().cargarSolicitudRodamientos(xml);
		}

		@Override
		public void altaRodamiento(RodamientoVO rod) throws RemoteException {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void actualizarStock(RodamientoVO rod, MovimientoStockVO mov)
				throws RemoteException {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void eliminarRodamiento(RodamientoVO rod) throws RemoteException {
			// TODO Auto-generated method stub
			
		}


		@Override
		public RodamientoVO buscarRodamiento(RodamientoVO rod) throws RemoteException {
			return getCPRIR().buscarRodamiento(rod);
		}

		@Override
		public void buscarRodamiento(int codigo) throws RemoteException {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void altaProveedor(ProveedorVO prov) throws RemoteException {
			// TODO Auto-generated method stub
			
		}

		@Override
		public ProveedorVO buscarProveedor(ProveedorVO prov)
				throws RemoteException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void bajaProveedor(ProveedorVO prov) throws RemoteException {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void administrarListaProveedor() throws RemoteException {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void armarComparativaPrecios() throws RemoteException {
			// TODO Auto-generated method stub
			
		}

		@Override
		public float calcularPorcentajeGanancia(RodamientoVO rod, float porc)
				throws RemoteException {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public void altaItemListaPrecios(ProveedorVO prov, String tipolista,
				ItemListaPreciosVO item) throws RemoteException {
			// TODO Auto-generated method stub
			
		}

		@Override
		public ListaPreciosVO altaListaPrecios(ProveedorVO prov,
				ListaPreciosVO lista) throws RemoteException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void altaListaPrecios(ProveedorVO prov, String xml)
				throws RemoteException {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void modificarRodamiento(RodamientoVO rodamiento)
				throws RemoteException {
			// TODO Auto-generated method stub
			
		}

		@Override
		public List<CondicionesPagoVO> getCondicionesPagoVO()
				throws RemoteException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public ListaPreciosCPRVO obtenerComparativaPrecios(String codigo,
				String marca, String pais) throws RemoteException {
			return getCPRIR().obtenerComparativaPrecios(codigo, marca, pais);
		}

		@Override
		public void setPorcentajeGananciaCPR(Float porcentaje)
				throws RemoteException {
			// TODO Auto-generated method stub
			
		}

		@Override
		public Float getPorcentajeGananciaCPR() throws RemoteException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public ArrayList<ClienteVO> getClientes() throws RemoteException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public ArrayList<FacturaVO> getFacturas() throws RemoteException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public ArrayList<SolicitudRodamientosVO> getSolicitudesrodamientos()
				throws RemoteException {
			// TODO Auto-generated method stub
			return getODVIR().getSolicitudesrodamientos();
		}

		@Override
		public ArrayList<CotizacionVO> getCoticaciones() throws RemoteException {
			// TODO Auto-generated method stub
			return getODVIR().getCoticaciones();
		}

		@Override
		public ArrayList<SolicitudRodamientosVO> getSolicitudesRodamientos()
				throws RemoteException {
			
			return getODVIR().getSolicitudesrodamientos();
		}

		@Override
		public void altacliente(ClienteVO cli) throws RemoteException {
			// TODO Auto-generated method stub
			
		}

		@Override
		public ClienteVO buscarcliente(String cuit) throws RemoteException {
			// TODO Auto-generated method stub
			return getODVIR().buscarcliente(cuit);
		}

		@Override
		public ClienteVO buscarcliente(int id) throws RemoteException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void bajacliente(int id) throws RemoteException {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void modificarcliente(ClienteVO cli) throws RemoteException {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void generarsolicitudrodamientos(ClienteVO cli, String sr_xml)
				throws RemoteException {
			// TODO Auto-generated method stub
			
		}

		@Override
		public RodamientoVO buscarrodamiento(String codigo)
				throws RemoteException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void aceptarCotizacion(CotizacionVO cot) throws RemoteException {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void facturar(SolicitudCompraCPRVO sc) throws RemoteException {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void conectivityTestODV() throws RemoteException {
			getODVIR().conectivityTestODV();
			
		}	
		
		 
		
	}